Skip to content

feat: introducing watsonx sql database wrapper and toolkit #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: main
Choose a base branch
from

Conversation

Mateusz-Switala
Copy link
Collaborator

The goal of this PR is to add WatsonxSQLDatabase and WatsonxSQLDatabaseToolkit to langchain_ibm.

Example

from langchain_ibm.utilities.sql_database import WatsonxSQLDatabase

wx_sql_database = WatsonxSQLDatabase(connection_id=postgres_sql_connection_id,
                                     **credentials,
                                     schema="public"
                                     )

from langchain_ibm import ChatWatsonx

chat_llama = ChatWatsonx(
    watsonx_client=wx_sql_database.watsonx_client,
    model_id="mistralai/mistral-medium-2505",
    params={"max_new_tokens": 200},
)

from langchain_ibm.agent_toolkits.sql_toolkit import WatsonxSQLDatabaseToolkit


toolkit = WatsonxSQLDatabaseToolkit(db=wx_sql_database, llm=chat_llama)


from langchain import hub

prompt_template = hub.pull("langchain-ai/sql-agent-system-prompt")
system_message = prompt_template.format(dialect="PostgreSQL", top_k=5)

from langgraph.prebuilt import create_react_agent

agent_executor = create_react_agent(chat_llama, toolkit.get_tools(), prompt=system_message)


example_query = "What city in Japan has the largest population? How much it is?"

events = agent_executor.stream(
    {"messages": [("user", example_query)]},
    stream_mode="values",
)
for event in events:
    event["messages"][-1].pretty_print()

Comment on lines 4 to 7

from langchain_core.caches import BaseCache as BaseCache
from langchain_core.callbacks import Callbacks as Callbacks
from langchain_core.language_models import BaseLanguageModel
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we are importing BaseCache as BaseCache etc. ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No needed, removed

Copy link
Collaborator

@KarolZm KarolZm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants